from django.db import models

# Create your models here.
from mlh.utils.models import BaseModel
from users.models import User


class NewsCategory(BaseModel):
    category_name = models.CharField(max_length=32, verbose_name="新闻分类名")

    class Meta:
        db_table = 'tb_news_category'
        verbose_name = '新闻分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category_name


class News(BaseModel):
    category_id = models.ForeignKey(NewsCategory, on_delete=models.PROTECT, verbose_name='新闻分类')
    clicks = models.IntegerField(verbose_name='点击量')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    title = models.CharField(max_length=256, verbose_name='新闻标题')
    content = models.TextField(verbose_name='新闻内容')
    news_image = models.CharField(max_length=128, verbose_name='新闻图片', blank=True)
    collection = models.BooleanField(verbose_name='收藏', default=False)
    is_delete = models.BooleanField(verbose_name='删除', default=True)
    follow = models.BooleanField(verbose_name='关注', default=True)
    news_time = models.DateTimeField(verbose_name='创建时间')

    class Meta:
        db_table = 'tb_news'
        verbose_name = '新闻'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class NewsComments(BaseModel):
    parent_id = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True,
                                  blank=True, verbose_name='自关联id')
    user = models.ForeignKey(User, related_name='user', on_delete=models.CASCADE, verbose_name='用户')
    news = models.ForeignKey(News, related_name='news', on_delete=models.CASCADE, verbose_name='新闻')
    comment = models.CharField(max_length=512, verbose_name='评论内容')

    class Meta:
        db_table = 'tb_news_comments'
        verbose_name = '评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.id)
